$(document).ready(function () {
    var nowuserid = window.location.search;
    var jqnum = nowuserid.indexOf('=');
    var jqnums = nowuserid.substr(jqnum + 1);
    var qj_userid = jqnums;
    var arr = ["热门微博", "微话题", "找人", "电影", "听歌", "股票", "播客", "视频", "旅游", "购物", "直播", "新闻"];
    var hotArr = ["视频", "厦门", "社会", "国际", "科技", "科普", "数码", "财经", "股市", "明星", "综艺", "电视剧", "电影", "音乐",
        "汽车", "体育", "运动健身", "健康", "瘦身", "养生", "军事", "历史"];
    //设置左边导航栏
    var str = "<ul>";
    for (var i = 0; i < arr.length; i++) {
        str += "<li>" + arr[i] + "</li>";
    }
    //设置右边导航栏
    var str2 = "";
    for (var i = 0; i < hotArr.length; i++) {
        str2 += "<li><img src='images/discover_right" + (i + 1) + ".png'/>" + hotArr[i] + "</li>";
    }
    $("#left").append(str + "</ul>");
    $(".dr_classes").append(str2);

    //等级图标
    function setUtter() {
        for (var i = 0; i < $(".utterer").length; i++) {
            $(".utterer").eq(i).children('a').eq(1).css('background-position', '2px -49px');
            $(".utterer").eq(i).children('a').eq(2).css('background-position', '-25px -100px');
            $(".utterer").eq(i).children('a').eq(3).css('background-position', '-25px -300px');
        }
    }

    //获取名字(userid)
    $.get('discoverServlert?op=getUser&userid=' + qj_userid, function (data, status) {
        $(".dc_username").html(data.MesgName);
    });

    //微博右上角下拉事件
    function blogRightTop() {
        $(".utterer i").click(function () {
            var num = $(".utterer i").index($(this));
            $(this).siblings('.others').slideToggle(400);
            for (var i = 0; i < $('.others').length; i++) {
                if (i != num) {
                    $('.others').eq(i).slideUp(200);
                }
            }
        });
    }

    //热门微博内容标题a标签点击事件
    var co = $("#right li");

    function move_drli(str) {
        // $(str).html(co);
        //$(str).append("<div class='clear'></div>");
    }

    //头部推荐，热门搜索，分类跳转事件!
    $(".groom").css('display', 'block');
    $(".cenTitle a").eq(0).css('font-weight', 'bold').css('border-bottom', '2px solid #f7691d');
    function setheadJump() {
        $(".cenTitle a").click(function () {
            $(this).css('font-weight', 'bold').css('border-bottom', '2px solid #f7691d');
            $(this).siblings('a').css('font-weight', 'normal').css('border-bottom', 'none');
            var ct_a = $(".cenTitle a").index($(this)) + 1;
            setTuiJian(ct_a);
        });
    }

    //点击评论跳出评论界面
    function discountEve() {
        $(".saySome").click(function () {
            var n = $(".blogfoot").index($(this).parent('.blogfoot'));
            var wid = $(".upDiscount").attr("title");
            $(".dc_discount").eq(n).fadeToggle(100);
            for (var i = 0; i < $(".dc_discount").length; i++) {
                if (i != n) {
                    $(".dc_discount").eq(i).hide();
                    $(".dc_faces").eq(i).hide();
                }
            }
            $('.discount_cont').val('');
            //加载评论页面
            $.ajax({
                type: "post",
                url: "discoverServlert?op=loadDiscuss",
                data: {"Sid":$(this).attr('title')},
                dataType:"json",
                success: function (data) {
                    $('.upDiscount').eq(n).children('.upDiscountUl').empty();
                    $.each(data, function (index, dm) {
                        $('.upDiscount').eq(n).children('.upDiscountUl').prepend("<li class='upDiscountLi'>" +
                            "<div class='udl_headPic'><img src='images/" + dm.Sid + "'></div>" +
                            "<div class='udl_cont'>" +
                            "<div class='udl_speaker'><a href='javascript:void(0)'>" + dm.Cuid + ":</a>" + dm.Ccontent + "</div>" +
                            "<div class='udl_time'><span href='javascript:void(0)'>" + dm.Cdate + wid + "</span>" +
                            "<div class='discu_zan'><span><em class='dis_zanpic'></em>赞</span><span>回复</span></div></div>" +
                            "</div>" +
                            "</li>");
                    });
                },
                error: function (data) {
                    alert("评论加载失败！")
                }
            })
        });

        $(".jumpFaces").click(function () {
            var num = $(".jumpFaces").index($(this));
            $(".dc_faces").eq(num).slideToggle(200);
        });
    }

    //添加表情
    function addfaces() {
        for (var i = 1; i <= 60; i++) {
            $(".dc_faces_list ul").append("<li class='facerq'><img src='images/dc_faces" + i + ".gif'></li>");
        }
        $(".facerq").click(function () {
            var n = $(".dc_faces_list ul").index($(this).parent("ul"));
            $(".discount_cont").eq(n).append($(this).html());
        });
    }

    //关闭表情
    function closefaces() {
        $(".faces_close a").click(function () {
            var n = $(".faces_close a").index($(this));
            $(".dc_faces").eq(n).hide();
        });
    }

    //评论点赞
    function giveZan() {
        $(".dis_zanpic").click(function () {
            alert(666);
            $(this).css('background-position', '22px 0px');

        });
    }

    //评论发布(userid)
    function usUDL() {
        $(".dc_dsthings a").click(function () {
            var num = $(".dc_dsthings a").index($(this));       //获取当前的评论按钮
            var wid = $(this).attr('title');                    //迷
            var udl_cont = $(".discount_cont").eq(num).html();  //获取评论栏内的内容
            //var userid = qj_userid;
            if (udl_cont != "") {
                $.ajax({
                    type: "post",
                    url: "discoverServlert?op=getUM",
                    data: {"udl_cont": udl_cont, "Cfor": $(".haha").html()},
                    success: function (data) {
                        alert("转发成功！")
                        $(".blackWrap").hide();
                        $(".dc_layer ").hide();
                    },
                    error: function (data) {
                        alert("转发失败！")
                    }
                })
            }
            $.get("discoverServlert?op=getUM", function (data, status) {
                var headimg = data.um.MesgHeadimg;
                var username = data.um.MesgName;
                console.log(headimg);
                if (udl_cont != "") {

                    $('.upDiscount').eq(num).children('.upDiscountUl').prepend("<li class='upDiscountLi'>" +
                        "<div class='udl_headPic'><img src='images/" + dm.Sid + "'></div>" +    //显示头像
                        "<div class='udl_cont'>" +
                        "<div class='udl_speaker'><a href='javascript:void(0)'>" + dm.Cuid + ":</a>" + dm.Ccontent + "</div>" + //显示评论内容
                        "<div class='udl_time'><span href='javascript:void(0)'>" + dm.Cdate + wid + "</span>" + //显示评论时间
                        "<div class='discu_zan'><span><em class='dis_zanpic'></em>赞</span><span>回复</span></div></div>" +    //...
                        "</div>" +
                        "</li>");
                    $(".discount_cont").html('');

                } else {
                    alert("评论内容不能为空！");
                }
            });

        });
    }

    //ajax获取数据往推荐页面布局布局
    function setTuiJian(bg_classify) {
        $('.groom').empty();
        move_drli(".dr_classes");
        $("#right").css('display', 'block');
        if (bg_classify == 2) {
            $('.groom').append("<div class='rink_list'>" +
                "<ul>" +
                "<li class='list_li_first'></li>" +
                "<li class='list_li'>24小时<em class='list_ramark'></em></li>" +
                "<li class='list_li'>1小时</li>" +
                "<li class='list_li'>周榜</li>" +
                "<li class='list_li'>月榜</li>" +
                "<li class='list_li_last'></li>" +
                "<i></i></ul>" +
                "</div>");
        } else if (bg_classify == 3) {
            $('.groom').append("<div class='hbc_classes'></div>");
            move_drli(".hbc_classes");
            $("#right").css('display', 'none');
        }
        $.get("discoverServlert?op=loadPage", function (data, status) {
            $.each(data, function (index, value) {
                //微博内容大体布局
                $('.groom').append("<div class='blogtext'><div class='bt_head'>" +
                    "<div class='headPic'></div><div class='blogcenter'></div></div>" +
                    "<div class='blogfoot'><div><img src='images/blogaction1.png'>收藏</div>" +
                    "<div class='transmit'><img src='images/blogaction2.png'>转发</div>" +
                    "<div  class='saySome' title='" + value.share.Sid + "'><img src='images/blogaction3.png' class='comment'>评论</div>" +
                    "<div class='last'><img src='images/blogaction4.png'>点赞</div></div></div>");
                //微博内容底部布局添加内容
                $(".headPic").eq(index).append("<img src=images/" + value.headimg + "></img>");
                //微博内容上部布局添加内容
                if (value.share.Shttp != null) {
                    $(".blogcenter").eq(index).append("<div class='utterer'>" +
                        "<a href='javascript:void(0)' class='haha' title=" + value.share.Uid + ">" + value.Uname + "</a>" +
                        "<a href='javascript:void(0)'></a>" +
                        "<a href='javascript:void(0)'></a>" +
                        "<a href='javascript:void(0)'></a><i></i>" +
                        "<div class='others'><li>帮上头条</li><li>举报</li></div></div>" +
                        "<div class='utmsg'><a href='javascript:void(0)'>" + value.share.Sdate + "</a> 来自：" +
                        "<a href='javascript:void(0)'>" + value.Uaddress + "</a></div>" +
                        "<div class='whoUtt'>" + value.share.Scontent + "</div><div class='uttContent'></div>" +
                        "<embed src='" + value.share.Shttp + "' width='480' height='400' type='application/x-shockwave-flash'> </embed>");
                } else {
                    $(".blogcenter").eq(index).append("<div class='utterer'>" +
                        "<a href='javascript:void(0)' class='haha' title=" + value.share.Uid + ">" + value.Uname + "</a>" +
                        "<a href='javascript:void(0)'></a>" +
                        "<a href='javascript:void(0)'></a>" +
                        "<a href='javascript:void(0)'></a><i></i>" +
                        "<div class='others'><li>帮上头条</li><li>举报</li></div></div>" +
                        "<div class='utmsg'><a href='javascript:void(0)'>" + value.share.Sdate + "</a> 来自：" +
                        "<a href='javascript:void(0)'>" + value.Uaddress + "</a></div>" +
                        "<div class='whoUtt'>" + value.share.Scontent + "</div><div class='uttContent'></div>");
                }

                // if (value.share.Shttp !== undefined) {
                //     $('.uttContent').eq(index).append("<img src='" + value.share.Wimg + "'></img>");
                // }
                $(".blogtext").eq(index).append("<div class='dc_discount'><div class='dc_feed'>" +
                    "<div class='dc_headPic' title=" + value.share.Uid + "><img src='images/" + value.headimg + "'></img></div>" +
                    "<div class='dc_publish'>" +
                    "<div name='' class='discount_cont' contenteditable='true'></div>" +
                    "<div class='dc_dsthings'></div>" +
                    "<div class='dc_faces'><div class='faces_title'><div class='faces_close'>" +
                    "<a href='javascript:void(0)'>X</a></div></div>" +
                    "<div class='dc_faces_list'><em class='outsanjiao'></em><em class='insanjiao'></em><ul></ul></div>" +
                    "</div></div>" +
                    "</div></div>");
                //评论界面
                $(".dc_dsthings").eq(index).append("<span class='dc_ico'><img src='images/dc_face.png' class='jumpFaces'></img>" +
                    "<img src='images/dc_upimg.png'></img></span><input type='checkbox' name='dc_foword' class='dc_forword'></input>" +
                    "<span class='dc_fwsp'>同时转发到微博1</span><a href='javascript:void(0)' title=" + value.share.Sid + ">评论</a>");
                $(".dc_discount").eq(index).append("<div class='upDiscount' title=" + value.share.Uid + ">" +
                    "<ul class='upDiscountUl'></ul></div>");
            });
            setUtter();
            blogRightTop();
            discountEve();
            addfaces();
            closefaces();
            giveZan();
            usUDL();
            setTransmit();
        });
    }

    setTuiJian(1);
    setheadJump();
    //左边导航点击效果
    $("#left li").eq(0).css('background-color', 'rgba(222,222,222,0.5)');
    $("#left li").click(function (event) {
        $(this).css('background-color', 'rgba(222,222,222,0.5)');
        $(this).siblings().css('background-color', '');
    });
    //window滚动事件
    $(document).scroll(function (event) {
        var topval = $(document).scrollTop();
        if (topval > 450) {
            $(".left").css('position', 'fixed');
        }
        else {
            $(".left").css('position', 'static');
        }
    });
    //右边各类模块跳转事件
    $(".rightCont").eq(0).css('display', 'block');
    $("#left li").click(function (event) {
        /* Act on the event */
        var row = $("#left li").index($(this));
        console.log(row);
        $(".rightCont").eq(row).css('display', 'block');
        $(".rightCont").eq(row).siblings('.rightCont').css('display', 'none')
    });


    function setTransmit() {
        //转发页面头部转发到事件
        $(".minitb_li a").eq(0).css({'background-color': 'white', 'border-bottom': '3px solid orange'});
        $(".minitb_li a").click(function () {
            var num = $(".minitb_li a").index($(this));
            $(this).css({'background-color': 'white', 'border-bottom': '3px solid orange'});
            for (var i = 0; i < 3; i++) {
                if (i != num) {
                    $(".minitb_li a").eq(i).css({'background-color': '', 'border-bottom': ''});
                }
            }
        });
        //打开转发页面
        $(".transmit").click(function (event) {
            $(".blackWrap").show();
            $(".dc_layer").show();
            console.log($(document).scrollTop());
            var n = $(document).scrollTop() + 100;
            $(".dc_layer").css('top', n + 'px');
        });

        //关闭转发页面
        $(".close_pic").click(function () {
            $(".dcl_teta").val("");
            $(".blackWrap").hide();
            $(".dc_layer ").hide();
        });
        //
        $(".last").click(function () {
            var zanimg = $(this).children("img").attr('src');
            if (zanimg == "images/blogaction4.png") {
                $(this).children("img").attr('src', 'images/dc_zan2.png');
            }
            else {
                $(this).children("img").attr('src', 'images/blogaction4.png');
            }
        });

    }

    //点击转发
    $(".dcl_btna").click(function () {
        $(".dcl_faces").click(function () {
            var num = $(".dcl_faces").index($(this));
            $(".dc_faces").eq(num).slideToggle(200);
        });
        addfaces();
        $.ajax({
            type: "post",
            url: "share",
            data: {"dcl_teta": $(".dcl_teta").val(), "Sfrom": $(".haha").html()},
            success: function (data) {
                alert("转发成功！")
                $(".blackWrap").hide();
                $(".dc_layer ").hide();
            },
            error: function (data) {
                alert("转发失败！")
            }
        })
    })

    // //点击评论
    // $(".comment").click(function () {
    //     $.ajax({
    //         type: "post",
    //         url: "discoverServlert?op=comment",
    //         data: {"comment": $(".comment").val()},
    //         success: function (data) {
    //             alert("评论成功！")
    //
    //         },
    //         error: function (data) {
    //             alert("评论失败！")
    //         }
    //
    //     })
    // })

    $("#indexlqh").click(function () {
        location.href = "index.html?userid=" + jqnums;
    });

});
